package com.macro.mall.controller;

import com.macro.mall.service.OmsOrderReturnReasonService;
import com.macro.model.OmsOrderReturnReason;
import common.api.CommonPage;
import common.api.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "退货原因",tags = "退货原因模块")
@Controller
@RequestMapping("/returnReason")
public class OmsOrderReturnReasonController {

    @Autowired
    private OmsOrderReturnReasonService reasonService;

    @ApiOperation("添加退货原因")
    @PostMapping("/create")
    @ResponseBody
    public CommonResult create(@RequestBody OmsOrderReturnReason reason){
        System.out.println("退货原因：" + reason.toString());
        int count = reasonService.create(reason);
        System.out.println("count:" + count);
        if (count > 0)
           return CommonResult.success(count);
        return CommonResult.failed();
    }

    @ApiOperation("修改退货原因")
    @PostMapping("/update/{id}")
    @ResponseBody
    public CommonResult update(@PathVariable("id") Long id,
                               @RequestBody OmsOrderReturnReason reason){
        int count = reasonService.update(id,reason);
        if(count > 0)
            return CommonResult.success(count);
        return CommonResult.failed();
    }

    @ApiOperation("删除退货原因")
    @PostMapping("/delete")
    @ResponseBody
    public CommonResult delete(@RequestParam("ids") List<Long> ids) {
        int count = reasonService.delete(ids);
        if(count > 0)
            return CommonResult.success(count);
        return CommonResult.failed();
    }

    @ApiOperation("分页查询全部退货原因")
    @GetMapping("/list")
    @ResponseBody
    public CommonResult<CommonPage<OmsOrderReturnReason>> list(@RequestParam("pageNum") Integer pageNum,
                                                               @RequestParam("pageSize") Integer pageSize){

        return CommonResult.success(CommonPage.restPage(reasonService.list(pageNum,pageSize)));
    }

    @ApiOperation("获取单个退货原因详情")
    @GetMapping("/list/{id}")
    @ResponseBody
    public CommonResult<OmsOrderReturnReason> getItem(@PathVariable("id") Long id){

        return CommonResult.success(reasonService.getItem(id));
    }

    @ApiOperation("批量修改退货原因启动状态")
    @PostMapping("/update/status")
    @ResponseBody
    public CommonResult updateStatus(@RequestParam("ids") List<Long> ids,
                                     @RequestParam("status") Integer status) {
        int count = reasonService.updateStatus(ids, status);
        if(count > 0)
            return CommonResult.success(count);
        return CommonResult.failed();
    }


}
